package com.it.mapper;

import com.it.pojo.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.List;

@Mapper
public interface StudentMapper {
    @Select("<script>" +
            "select s.id, snum, name, cid, cname, tid, tname, state, number, grade\n" +
            "from student s, team t, classes c\n" +
            "where s.tid = t.id and t.cid = c.id\n" +
            "<if test='classes != null and classes != \"\"'> " +
            "and t.cid = #{classes}</if>" +
            "<if test='team != null and team != \"\"'> " +
            "and s.tid = #{team}</if>" +
            "order by tid" +
            "</script>")
    List<Student> selectByCondition(@Param("classes") Integer classes, @Param("team") Integer team);

    @Select("<script>" +
            "select s.id, snum, name, cid, cname, tid, tname, state, number, grade\n" +
            "from student s, team t, classes c\n" +
            "where s.tid = t.id and t.cid = c.id and s.state = 1\n" +
            "<if test='classes != null and classes != \"\"'> " +
            "and t.cid = #{classes}</if>" +
            "<if test='team != null and team != \"\"'> " +
            "and s.tid = #{team}</if>" +
            "order by tid" +
            "</script>")
    List<Student> selectByConditionX(@Param("classes") Integer classes, @Param("team") Integer team);

    @Update("update student set number = number+1 where id = #{id}")
    void addNumber(Long id);

    /**********后台管理系统***********/

    @Select("select * from admin where username = #{username} and password = #{password}")
    Admin login(String username, String password);

    @Insert("insert into student(snum, name, tid) values(#{snum}, #{name}, #{tid})")
    void add(Student s);

    @Update("update student set snum = #{snum}, name = #{name}, tid = #{tid}, state = #{state}, grade = #{grade} where id=#{id}")
    void update(Student s);

    @Delete("delete from student where id=#{id}")
    void delete(Integer id);

}
